

from django.shortcuts import render
from stuInfo.models import StudentInfo
from django.core.paginator import Paginator

def index(request,pg=10,pn=1):
    if request.method == "GET":
        pg = request.GET.get('pg',10)
        pn = request.GET.get('pn',1)
        data = StudentInfo.objects.all()
        schools = StudentInfo.objects.values('上课院系').distinct()
        paginator = Paginator(data,pg)
        current_page = paginator.page(pn)
        return render(request, 'index.html',{
            "datas":current_page,
            "pagenumber":pn,
            "schools":schools
        })
    

def query(request,pg=10,pn=1):
    if request.method == "POST":
        global schools
        schools = StudentInfo.objects.values('上课院系').distinct()
        global school_name
        school_name = request.POST.get("school_name")
        global name
        name = request.POST.get("name")
        global car_no
        car_no = request.POST.get("car_no")
        global data
        if school_name != '--请选择学院--' and name == '' and car_no == '':
            data = StudentInfo.objects.filter(上课院系=school_name)
        elif school_name == '--请选择学院--' and name !='' and car_no == '':
            data = StudentInfo.objects.filter(姓名__contains=name)
        elif school_name == '--请选择学院--' and name == '' and car_no != '':
            data = StudentInfo.objects.filter(身份证件号__contains=car_no)
        elif school_name != '--请选择学院--' and name != '' and car_no == '':
            data = StudentInfo.objects.filter(上课院系=school_name).filter(姓名__contains=name)
        elif school_name != '--请选择学院--' and name == '' and car_no !='':
            data = StudentInfo.objects.filter(上课院系=school_name).filter(身份证件号__contains=car_no)
        elif school_name == '--请选择学院--' and name != '' and car_no != '':
            data = StudentInfo.objects.filter(姓名__contains=name).filter(身份证件号__contains=car_no)
        elif school_name != '--请选择学院--' and name != '' and car_no != '':
            data = StudentInfo.objects.filter(姓名__contains=name) \
                                .filter(身份证件号__contains=car_no) \
                                .filter(上课院系=school_name)
        else:
            data = StudentInfo.objects.filter(身份证件号=car_no)
            
        paginator = Paginator(data,pg)
        current_page = paginator.page(pn)
        return render(request, 'query.html',{
                "datas":current_page,
                "pagenumber":pn,
                "schools":schools,
                "school_name":school_name,
                "name":name,
                "car_no":car_no
            })
    pg = request.GET.get('pg',10)
    pn = request.GET.get('pn',1)
    # schools = StudentInfo.objects.values('上课院系').distinct()
    paginator = Paginator(data,pg)
    current_page = paginator.page(pn)
    return render(request, 'query.html',{
        "datas":current_page,
        "pagenumber":pn,
        "schools":schools,
        "school_name":school_name,
        "name":name,
        "car_no":car_no
    })